//
//  ViewController.m
//  Homework_18
//
//  Created by 徐昊 on 16/8/8.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "CustomTableViewCell.h"
#import "AddViewController.h"
#import "Contact.h"
#define kTableViewCellName @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddViewControllerDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contactArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _contactArray = [NSMutableArray array];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _contactArray.count;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    AddViewController *add = segue.destinationViewController;
    add.delegate = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellName];
    cell.contactPhoto.image = [_contactArray[indexPath.row] contactImage];
    cell.name.text = [_contactArray[indexPath.row] name];
    cell.phoneNumber.text = [_contactArray[indexPath.row] phoneNumber];
    return cell;
}
- (void)addContact:(Contact *)contact {
    if ((contact.name != nil) && (contact.phoneNumber != nil)) {
        [_contactArray addObject:contact];
        [_tableView reloadData];
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
